package com.quasar.hpatient.module.home_daily;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.home_all.AllItemBean;
import com.quasar.hpatient.bean.home_all.AllNetBean;
import com.quasar.hpatient.bean.home_inspection.CalendarAllBean;
import com.quasar.hpatient.bean.home_inspection.CalendarBean;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.module.home_bloodpressure.BloodpressureActivity;
import com.quasar.hpatient.module.home_bloodsugar.BloodsugarActivity;
import com.quasar.hpatient.module.home_heartrate.HeartrateActivity;
import com.quasar.hpatient.module.home_inspection_chart.HomeInspectionChartActivity;
import com.quasar.hpatient.module.home_temperature.TemperatureActivity;
import com.quasar.hpatient.module.home_urinevolume.UrinevolumeActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import lib.quasar.base.cache.SpManager;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.base.frame.OnModelChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.edit.AlterEditText;
import okhttp3.RequestBody;

/**
 * description: 首页 ==> 全部
 * created by kalu on 2018/4/2 11:12
 */
public class HomeDailyPresenter implements BasePresenter {

    private final Map<String, AllItemBean> mDatasOther = new HashMap<>();
    private final LinkedList<AllItemBean> mData = new LinkedList<>();
    private String json_other_height = "json_other_height";
    private String json_other_weight = "json_other_weight";
    private String json_other_bmi = "json_other_bmi";
    private String json_other_sleep_time = "json_other_sleep_time";
    private String json_other_amount_exercise = "json_other_amount_exercise";
    private String json_other_traffic = "json_other_traffic";
    private String json_other_bloodsugar = "json_other_bloodsugar";
    private String sp_file_name = "dailyconfig";
    private String BloodsugarFlag = "BloodsugarFlag";
    private String TrafficFlag = "TrafficFlag";
    private boolean removeBloodsugarFlag = true;
    private boolean removeTrafficFlag = true;
    private AllNetBean allNetBean = null;

    public boolean isRemoveBloodsugarFlag() {
        return removeBloodsugarFlag;
    }

    public void setRemoveBloodsugarFlag(boolean removeBloodsugarFlag) {
        this.removeBloodsugarFlag = removeBloodsugarFlag;
    }

    public boolean isRemoveTrafficFlag() {
        return removeTrafficFlag;
    }

    public void setRemoveTrafficFlag(boolean removeTrafficFlag) {
        this.removeTrafficFlag = removeTrafficFlag;
    }
    public AllNetBean getAllNetBean() {
        return allNetBean;
    }

    public void setAllNetBean(AllNetBean allNetBean) {
        this.allNetBean = allNetBean;
    }
    void initData(){

        SpManager sp = SpManager.getInstance(sp_file_name);
        setRemoveBloodsugarFlag(sp.getBoolean(BloodsugarFlag,true));
        setRemoveTrafficFlag(sp.getBoolean(TrafficFlag,true));

    }

    void initTime(final HomeDailyView view, TextView dates) {

        if (null != dates) {
            view.setText(dates, getCalendar());
        }
    }



    void initList(HomeDailyView view, RecyclerView recycler) {

        if (null == view || null == recycler)
            return;

        initmDatas(view);

        //recycler.setTag(false);
        final BaseCommonAdapter<AllItemBean> adapter = new BaseCommonAdapter<AllItemBean>(mData, R.layout.activity_home_daily_child) {

            @Override
            protected void onNext(RecyclerHolder holder, AllItemBean model, int position) {

                final AlterEditText edit = holder.getView(R.id.home_daily_input);
                edit.setNumberFilter(1,7);
                holder.setOnLongClickListener(v -> {
                    if (!model.isLongPress()) return true;
                    view.showDelete(position, model.getRecord_name());
                    return true;
                }, R.id.home_all_child_name, R.id.home_daily_input, R.id.home_all_child_unit);

                edit.setOnInputChangeListener((input, isModelChange,isMax) -> {

                    final Object tag = recycler.getTag();
                    if (isModelChange && (null == tag || ((boolean) tag) == false)) {
                        recycler.setTag(isModelChange);
                    }
                    model.setRecord_value(input);

                    if (position == 7 || position == 8) {

                        final AllItemBean bimModel = mData.get(9);
                        if (null == bimModel)
                            return;

                        if (TextUtils.isEmpty(input)) {
                            bimModel.setRecord_value("");
                            final View child = recycler.getLayoutManager().findViewByPosition(9);
                            if (null != child) {
                                final EditText edits = child.findViewById(R.id.home_daily_input);
                                edits.setText("");
                            }
                        } else {

                            try {
                                String height, weight;
                                if (position == 7) {
                                    height = input;
                                    weight = mData.get(8).getRecord_value();
                                } else {
                                    height = mData.get(7).getRecord_value();
                                    weight = input;
                                }

                                final float heights = Float.parseFloat(height);
                                final float weights = Float.parseFloat(weight);

                                if (heights != 0f && weights != 0f) {

                                    float bmis = (weights / ((heights / 100) * (heights / 100)));
                                    String str = String.valueOf(bmis);

                                    final int i = str.indexOf(".");
                                    if (i != -1 && (str.length() - i) > 2) {
                                        str = str.substring(0, i + 2);
                                    }

                                    bimModel.setRecord_value(str);
                                    final View child = recycler.getLayoutManager().findViewByPosition(9);
                                    if (null != child) {
                                        final EditText edits = child.findViewById(R.id.home_daily_input);
                                        edits.setText(str);
                                    }
                                } else {
                                    bimModel.setRecord_value("");
                                    final View child = recycler.getLayoutManager().findViewByPosition(9);
                                    if (null != child) {
                                        final EditText edits = child.findViewById(R.id.home_daily_input);
                                        edits.setText("");
                                    }
                                }
                            } catch (Exception e) {
                                bimModel.setRecord_value("");
                                final View child = recycler.getLayoutManager().findViewByPosition(9);
                                if (null != child) {
                                    final EditText edits = child.findViewById(R.id.home_daily_input);
                                    edits.setText("");
                                }
                            }
                        }
                    }
                });

                view.setText(holder, R.id.home_all_child_name, model.getRecord_name());
                view.setTextColor(holder, R.id.home_all_child_name, model.getColor());
                view.setText(holder, R.id.home_all_child_unit, model.getRecord_dose());
                view.setTextColor(holder, R.id.home_all_child_unit, model.getColor());

                //final AlterEditText edit = holder.getView(R.id.home_daily_input);
                edit.setText(model.getRecord_value());
                //  edit.setTextColor(model.getColor());
                edit.setNumberFilter(1, 7);
                edit.setModelText(model.getRecord_value());
                edit.setFocusable(position == 9 || position <= 6 ? false : true);
                edit.setCursorVisible(position == 9 || position <= 6 ? false : true);
                edit.setFocusableInTouchMode(position == 9 || position <= 6 ? false : true);
                holder.setOnClickListener(R.id.home_all_child_name, v -> {
                    final Intent intent = new Intent();
                    int positionNum = position;
                    if (position >= 2){
                        positionNum = isRemoveBloodsugarFlag()?position:position+1;
                    }
                    switch (positionNum) {
                        case 0:
                            intent.setClass(BaseApp.getContext(), TemperatureActivity.class);
                            intent.putExtra(TemperatureActivity.MULIT, true);
                            intent.putExtra(TemperatureActivity.DATE, view.getDate());
                            break;
                        case 1:
                            intent.setClass(BaseApp.getContext(), BloodpressureActivity.class);
                            intent.putExtra(BloodpressureActivity.MULIT, true);
                            intent.putExtra(BloodpressureActivity.DATE, view.getDate());
                            break;
                        case 2:
                            intent.setClass(BaseApp.getContext(), BloodsugarActivity.class);
                            intent.putExtra(BloodsugarActivity.MULIT, true);
                            intent.putExtra(BloodsugarActivity.DATE, view.getDate());
                            break;
                        case 3:
                            intent.setClass(BaseApp.getContext(), HeartrateActivity.class);
                            intent.putExtra(HeartrateActivity.MULIT, true);
                            intent.putExtra(HeartrateActivity.DATE, view.getDate());
                            break;
                        case 4:
                        case 5:
                        case 6:
                            intent.setClass(BaseApp.getContext(), UrinevolumeActivity.class);
                            intent.putExtra(UrinevolumeActivity.MULIT, false);
                            intent.putExtra(UrinevolumeActivity.DATE, view.getDate());
                            break;
                        default:
                            intent.setClass(BaseApp.getContext(), HomeInspectionChartActivity.class);
                            break;
                    }
                    view.goNext(intent);
                });

                holder.setOnClickListener(R.id.home_daily_input, v -> {

                    if(position>6)
                        return;

                    final Intent intent = new Intent();
                     int positionNum = position;
                     if (position >= 2){
                         positionNum = isRemoveBloodsugarFlag()?position:position+1;
                     }

                    switch (positionNum) {
                        case 0:
                            intent.setClass(BaseApp.getContext(), TemperatureActivity.class);
                            intent.putExtra(TemperatureActivity.MULIT, false);
                            intent.putExtra(TemperatureActivity.DATE, view.getDate());
                            view.goNext(intent);
                            break;
                        case 1:
                            intent.setClass(BaseApp.getContext(), BloodpressureActivity.class);
                            intent.putExtra(BloodpressureActivity.MULIT, false);
                            intent.putExtra(BloodpressureActivity.DATE, view.getDate());
                            view.goNext(intent);
                            break;
                        case 2:
                            intent.setClass(BaseApp.getContext(), BloodsugarActivity.class);
                            intent.putExtra(BloodsugarActivity.MULIT, false);
                            intent.putExtra(BloodsugarActivity.DATE, view.getDate());
                            view.goNext(intent);
                            break;
                        case 3:
                            intent.setClass(BaseApp.getContext(), HeartrateActivity.class);
                            intent.putExtra(HeartrateActivity.MULIT, false);
                            intent.putExtra(HeartrateActivity.DATE, view.getDate());
                            view.goNext(intent);
                            break;
                        case 4:
                        case 5:
                        case 6:
                            intent.setClass(BaseApp.getContext(), UrinevolumeActivity.class);
                            intent.putExtra(UrinevolumeActivity.MULIT, false);
                            intent.putExtra(UrinevolumeActivity.DATE, view.getDate());
                            view.goNext(intent);
                            break;
                    }
                });



            }
        };

        // adapter.setNullView(BaseApp.getContext(), R.layout.layout_recycler_loading);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    private void initmDatas(HomeDailyView view) {
         mData.clear();
        for (int i = 0; i < 13; i++) {
            final AllItemBean model = new AllItemBean();
            model.setRecord_date(getDate(view.getDate()));
            model.setRecord_datetime(getDailyMinute(view.getDate()));
            //mData.add(model);
            switch (i) {
                case 0:
                    model.setRecord_name("体温");
                    model.setRecord_dose("°C");
                    mData.add(model);
                    break;
                case 1:
                    model.setRecord_name("血压");
                    model.setRecord_dose("mmHg");
                    mData.add(model);
                    break;
                case 2:
                    model.setRecord_name("血糖");
                    model.setRecord_dose("mmol/L");
                    if (isRemoveBloodsugarFlag())
                        mData.add(model);
                    break;
                case 3:
                    model.setRecord_name("心率");
                    model.setRecord_dose("次/分钟");
                    mData.add(model);
                    break;
                case 4:
                    model.setRecord_name("总入量");
                    model.setRecord_dose("ml");
                    mData.add(model);
                    break;
                case 5:
                    model.setRecord_name("白天尿量");
                    model.setRecord_dose("ml");
                    mData.add(model);
                    break;
                case 6:
                    model.setRecord_name("夜尿量");
                    model.setRecord_dose("ml");
                    mData.add(model);
                    break;
                case 7:
                    model.setRecord_name(json_other_height);
                    model.setRecord_dose("cm");
                    mData.add(model);
                    mDatasOther.put(json_other_height, model);
                    break;
                case 8:
                    model.setRecord_name(json_other_weight);
                    model.setRecord_dose("Kg");
                    mData.add(model);
                    mDatasOther.put(json_other_weight, model);
                    break;
                case 9:
                    model.setRecord_name(json_other_bmi);
                    model.setRecord_dose("-");
                    mData.add(model);
                    mDatasOther.put(json_other_bmi, model);
                    break;
                case 10:
                    model.setRecord_name(json_other_sleep_time);
                    model.setRecord_dose("小时");
                    mData.add(model);
                    mDatasOther.put(json_other_sleep_time, model);
                    break;
                case 11:
                    model.setRecord_name(json_other_amount_exercise);
                    model.setRecord_dose("km");
                    mData.add(model);
                    mDatasOther.put(json_other_amount_exercise, model);
                    break;
                case 12:
                    model.setRecord_name(json_other_traffic);
                    model.setRecord_dose("ml");
                    if (isRemoveTrafficFlag())
                        mData.add(model);
                    mDatasOther.put(json_other_traffic, model);
                    break;
            }
        }
    }

    final void schemelList(CalendarDialog dialog, String year, String month, String minDay, String maxDay) {

        final HashMap<String, Object> map = new HashMap<>();
        map.put(BEGIN_DATE, BaseApp.getResource().getString(R.string.date_calendar, year, month, minDay));
        map.put(END_DATE, BaseApp.getResource().getString(R.string.date_calendar, year, month, maxDay));

        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_3_8);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().schemeAllList(params);

        request(observable, new OnModelAcceptChangeListener<List<CalendarAllBean>>() {
            @Override
            public void modelSucc(List<CalendarAllBean> list) {
                dialog.getCalendarView().setScheme(list);
            }
        });
    }

    public void dailyList(final HomeDailyView view, RecyclerView recycler, boolean preview, boolean next) {

        if (null == view || null == recycler) return;

        final HashMap<String, Object> map = new HashMap<>();

        if (!preview && !next) {

            final String trim = view.getDate();
            if (TextUtils.isEmpty(trim)) {
                view.toast("时间格式错误");
                return;
            }
        }

        final String recordate = view.getDate();
        map.put(HttpParams.RECORD_DATE, getDate(recordate));

        final String datatype = (!preview && !next ? HttpParams.DATA_TYPE_3_4 : (preview ? HttpParams.DATA_TYPE_3_6 : HttpParams.DATA_TYPE_3_7));
        final RequestBody params = createParams(map, datatype);

        final Observable observable = HttpClient.getSingleHolder().getHttpService().listAll(params);
        request(observable, new OnModelAcceptChangeListener<AllNetBean>() {

            @Override
            public void modelSucc(AllNetBean result) {

                if (null == result)
                    return;
                setAllNetBean(result);

                for (AllItemBean model : mData) {
                    model.setRecord_value("");
                    model.setRecord_date(getDate(view.getDate()));
                    model.setRecord_datetime(getDailyMinute(view.getDate()));
                }

                final String record_date = result.getRecord_date();
                if (!TextUtils.isEmpty(record_date)) {
                    view.setDate(record_date);
                }

                final String datetime = view.getDate();

                // 体温
                final Map<String, List<AllItemBean>> map1 = result.getJson_temperature();
                if (null != map1 && map1.size() > 0 && map1.containsKey(getDate(datetime))) {
                    final List<AllItemBean> list = map1.get(getDate(datetime));
                    if (null != list && list.size() > 0) {
                        mData.get(0).setInput(list.get(0).getRecord_value());
                    }
                }

                // 血压
                final Map<String, List<AllItemBean>> map2 = result.getJson_bloodpressure();
                if (null != map2 && map2.size() > 0 && map2.containsKey(getDate(datetime))) {
                    final List<AllItemBean> list = map2.get(getDate(datetime));
                    if (null != list && list.size() > 0) {
                        mData.get(1).setInput(list.get(0).getBloodpressure());
                    }
                }

                // 血糖
                if (isRemoveBloodsugarFlag()) {
                    final Map<String, List<AllItemBean>> map3 = result.getJson_bloodsugar();
                    if (null != map3 && map3.size() > 0 && map3.containsKey(getDate(datetime))) {
                        final List<AllItemBean> list = map3.get(getDate(datetime));
                        if (null != list && list.size() > 0) {
                            mData.get(2).setInput(list.get(0).getRecord_value());
                        }
                    }
                }

                // 心率
                final Map<String, List<AllItemBean>> map4 = result.getJson_heartrate();
                if (null != map4 && map4.size() > 0 && map4.containsKey(getDate(datetime))) {
                    final List<AllItemBean> list = map4.get(getDate(datetime));
                    if (null != list && list.size() > 0) {
                        if (isRemoveBloodsugarFlag())
                            mData.get(3).setInput(list.get(0).getRecord_value());
                        else
                            mData.get(2).setInput(list.get(0).getRecord_value());
                    }
                }

                // 出入量
                final Map<String, List<AllItemBean>> map5 = result.getJson_urinevolume();
                if (null != map5 && map5.size() > 0 && map5.containsKey(getDate(datetime))) {

                    final List<AllItemBean> list = map5.get(getDate(datetime));
                    if (null != list && list.size() > 0) {

                        for (AllItemBean model : list) {

                            if (model.isInput()) {

                                final AllItemBean input =isRemoveBloodsugarFlag()? mData.get(4):mData.get(3);
                                String input1 = input.getRecord_value();
                                if (TextUtils.isEmpty(input1)) {
                                    input1 = "0";
                                }

                                String record_input = model.getRecord_input();
                                if (TextUtils.isEmpty(record_input)) {
                                    record_input = "0";
                                }

                                try {
                                    final int old = Integer.parseInt(input1);
                                    final int news = Integer.parseInt(record_input);
                                    input.setRecord_value(String.valueOf(old + news));
                                } catch (Exception e) {
                                    LogUtil.e("", e.getMessage(), e);
                                }

                            } else if (model.isOutput()) {

                                final AllItemBean output = isRemoveBloodsugarFlag()? mData.get(5):mData.get(4);
                                String output1 = output.getRecord_value();
                                if (TextUtils.isEmpty(output1)) {
                                    output1 = "0";
                                }

                                String record_output = model.getRecord_output();
                                String record_night = model.getRecord_night();
                                if (TextUtils.isEmpty(record_output)) {
                                    record_output = "0";
                                }

                                if (TextUtils.isEmpty(record_night)) {
                                    record_night = "0";
                                }

                                try {
                                    final int old = Integer.parseInt(output1);
                                    int news = Integer.parseInt(record_output);
                                    news += Integer.parseInt(record_night);
                                    final String s = String.valueOf(old + news);
                                    output.setRecord_value(s);
                                } catch (Exception e) {
                                }

                            } else if (model.isNight()) {

                                final AllItemBean night = isRemoveBloodsugarFlag()? mData.get(6):mData.get(5);
                                String night1 = night.getRecord_value();
                                if (TextUtils.isEmpty(night1)) {
                                    night1 = "0";
                                }

                                String record_night = model.getRecord_night();
                                if (TextUtils.isEmpty(record_night)) {
                                    record_night = "0";
                                }

                                try {
                                    final int old = Integer.parseInt(night1);
                                    final int news = Integer.parseInt(record_night);
                                    night.setRecord_value(String.valueOf(old + news));
                                } catch (Exception e) {
                                    LogUtil.e("", e.getMessage(), e);
                                }
                            }
                        }
                    }
                }

                final Map<String, AllItemBean> json_other = result.getJson_other();
                if (null == json_other) return;

                // 身高
                final AllItemBean itemBean6 = json_other.get(json_other_height);
                if (null != itemBean6 ) {

                    final AllItemBean model = isRemoveBloodsugarFlag()? mData.get(7):mData.get(6);
                        model.setRecord_date(getDate(datetime));
                        model.setRecord_dose(itemBean6.getRecord_dose());
                        model.setRecord_value(itemBean6.getRecord_value());

                }

                // 体重
                final AllItemBean itemBean7 = json_other.get(json_other_weight);
                if (null != itemBean7 ) {

                    final AllItemBean model = isRemoveBloodsugarFlag()? mData.get(8):mData.get(7);

                        model.setRecord_date(getDate(datetime));
                        model.setRecord_dose(itemBean7.getRecord_dose());
                        model.setRecord_value(itemBean7.getRecord_value());

                }

                final AllItemBean itemBean8 = json_other.get(json_other_bmi);
                if (null != itemBean8 ) {

                    final AllItemBean model = isRemoveBloodsugarFlag()? mData.get(9):mData.get(8);

                        model.setRecord_date(getDate(datetime));
                        model.setRecord_dose(itemBean8.getRecord_dose());
                        model.setRecord_value(itemBean8.getRecord_value());

                }


                final AllItemBean itemBean9 = json_other.get(json_other_sleep_time);
                if (null != itemBean9 ) {

                    final AllItemBean model = isRemoveBloodsugarFlag()? mData.get(10):mData.get(9);

                        model.setRecord_date(getDate(datetime));
                        model.setRecord_dose(itemBean9.getRecord_dose());
                        model.setRecord_value(itemBean9.getRecord_value());

                }

                final AllItemBean itemBean10 = json_other.get(json_other_amount_exercise);
                if (null != itemBean10 ) {

                    final AllItemBean model = isRemoveBloodsugarFlag()? mData.get(11):mData.get(10);

                        model.setRecord_date(getDate(datetime));
                        model.setRecord_dose(itemBean10.getRecord_dose());
                        model.setRecord_value(itemBean10.getRecord_value());

                }

                if (isRemoveTrafficFlag()) {
                    final AllItemBean itemBean11 = json_other.get(json_other_traffic);
                    if (null != itemBean11) {

                        final AllItemBean model = mData.getLast();

                            model.setRecord_date(getDate(datetime));
                            model.setRecord_dose(itemBean11.getRecord_dose());
                            model.setRecord_value(itemBean11.getRecord_value());

                    }
                }

            }

            @Override
            public void modelComplete() {
                view.refreshList(recycler);
            }
        });
    }

    void deleteTraffic(HomeDailyView view, RecyclerView recycler, int position, TextView date, String str) {

        if (null == view || null == recycler || null == date) return;

        final String trim = date.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            view.toast("时间格式错误");
            return;
        }
        SpManager sp = SpManager.getInstance(sp_file_name);
        if ("血糖".equals(str)) {
            removeBloodsugarFlag = false;
            sp.putBoolean(BloodsugarFlag,false);
            sp.apply();
            mData.remove(2);

        } else {
            removeTrafficFlag = false;
            sp.putBoolean(TrafficFlag,false);
            sp.apply();
            mData.removeLast();
            mDatasOther.remove(json_other_traffic);

        }


        view.refreshList(recycler);
    }

    void saveDaily(HomeDailyView view, RecyclerView recycler) {

        if (null == view || null == recycler)
            return;

        // bmi容错
        final AllItemBean bmiModel = mData.get(9);
        final String bmiValue = bmiModel.getRecord_value();
        if (!TextUtils.isEmpty(bmiValue)) {
            final float bmis = Float.parseFloat(bmiValue);
            if (bmis < 10 || bmis > 50) {
                mData.get(7).setRecord_value("");
                mData.get(8).setRecord_value("");
                bmiModel.setRecord_value("");
                view.toast("请输入正确的身高,体重");
                view.refreshList(recycler);
                return;
            }
        }

        final String datetime = view.getDate();
        if (TextUtils.isEmpty(datetime)) {
            view.toast("时间格式错误");
            return;
        }


        final HashMap<String, Object> params2 = new HashMap<>();
        params2.put(HttpParams.RECORD_DATE, getDate(datetime));
        params2.put(HttpParams.DATAS, mDatasOther);
        params2.put(HttpParams.DAILY_ITEMNAME, HttpParams.JSON_OTHER);
        params2.put(HttpParams.WARN_SIGN, "0");

        final RequestBody params1 = createParams(params2, HttpParams.DATA_TYPE_3_1);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().saveTemperature(params1);

        request(observable, new OnModelChangeListener() {

            @Override
            public void modelFail() {
                view.toast("保存日常记录失败");
            }

            @Override
            public void modelSucc() {
                recycler.setTag(false);
                view.toast("保存日常记录成功");
            }
        });
    }

    void addModel(HomeDailyView view, RecyclerView recycler,String name) {
        if (null == view || null == recycler) return;
        final String datetime = view.getDate();
        SpManager sp = SpManager.getInstance(sp_file_name);
        if ("血  糖".equals(name)) {
             final AllItemBean model = new AllItemBean();
             model.setRecord_name("血糖");
             model.setRecord_dose("mmol/L");
             model.setRecord_date(getDate(view.getDate()));
             model.setRecord_datetime(getDailyMinute(view.getDate()));
             mData.add(2,model);
             removeBloodsugarFlag = true;
             sp.putBoolean(BloodsugarFlag,true);
             sp.apply();
             if (null != getAllNetBean()){
                   final Map<String, List<AllItemBean>> item = getAllNetBean().getJson_bloodsugar();
                   if (null != item && item.size() > 0 && item.containsKey(getDate(datetime))) {
                       final List<AllItemBean> list = item.get(getDate(datetime));
                       if (null != list && list.size() > 0) {
                           mData.get(2).setInput(list.get(0).getRecord_value());
                       }
                   }

             }
        }
         if ("引流量".equals(name)) {
              final AllItemBean model2 = new AllItemBean();
              model2.setRecord_name("引流量");
              model2.setRecord_dose("ml");
              model2.setRecord_date(getDate(view.getDate()));
              model2.setRecord_datetime(getDailyMinute(view.getDate()));
              mData.addLast(model2);
              mDatasOther.put(json_other_traffic, model2);
              removeTrafficFlag = true;
              sp.putBoolean(TrafficFlag,true);
              sp.apply();
              if (null != getAllNetBean()) {
                  final Map<String, AllItemBean> json_other = getAllNetBean().getJson_other();
                  if (null != json_other){
                     final AllItemBean item1 = json_other.get(json_other_traffic);
                     if (null != item1) {
                         final AllItemBean model = mData.getLast();

                             model.setRecord_date(getDate(datetime));
                             model.setRecord_dose(item1.getRecord_dose());
                             model.setRecord_value(item1.getRecord_value());
                         
                     }

                  }
              }
         }

        view.refreshList(recycler);
    }

    final void refreshModel(HomeDailyView view, RecyclerView recycler, String input, int position) {

        if (null == view || null == recycler || TextUtils.isEmpty(input) || position == -1)
            return;

        final AllItemBean model = mData.get(position);
        model.setRecord_value(input);
        view.refreshList(recycler);
    }

    boolean canAdd() {
        return null != mData && mData.size() < 13;
    }

    @Override
    public void recycler() {
        mData.clear();
    }

}
